import { CustomForm } from "@/components/ProForm/interface";

export const useForm = () => {
  const addForm = reactive<CustomForm>({
    save: () => null,
    form: {
      principal: "",
      startPrice: "",
      endPrice: "",
      yield: "",
      profit: ""
    },
    options: [
      {
        hide: false,
        label: "基础信息",
        type: "title"
      },
      {
        type: "input",
        label: "买入价格",
        hide: false,
        prop: "startPrice",
        attrs: {
          type: "digit"
        },
        correctionParams: {
          digit: 2
        },
        inputMethod: () => changeInput()
      },
      {
        type: "input",
        label: "卖出价格",
        hide: false,
        prop: "endPrice",
        attrs: {
          type: "digit"
        },
        correctionParams: {
          digit: 2
        },
        inputMethod: () => changeInput()
      },
      {
        type: "input",
        hide: false,
        label: "本金",
        prop: "principal",
        defaultValue: "",
        children: [],
        inputMethod: () => changeInput()
      },
      {
        type: "input",
        hide: false,
        label: "利率",
        prop: "yield",
        defaultValue: "",
        children: [],
        attrs: {
          disabled: true
        }
      },
      {
        type: "input",
        hide: false,
        label: "盈利",
        prop: "profit",
        defaultValue: "",
        children: [],
        attrs: {
          disabled: true
        }
      }
    ]
  });

  const changeInput = () => {
    nextTick(() => {
      const { principal, startPrice, endPrice } = addForm.form;
      if (startPrice && endPrice) {
        console.log(parseFloat(endPrice), parseFloat(startPrice), endPrice, startPrice);

        const yieldValue = calculateProfit(startPrice, endPrice);
        if (principal) {
          addForm.form.profit = principal * (yieldValue + 1);
        }
        addForm.form.yield = yieldValue * 100 + "%";
      }
    });
  };

  return {
    addForm
  };
};
